package com.zz.framework.core.utils.file.sandbox

import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import com.zz.framework.core.utils.sandbox.FileLegacyImpl
import com.zz.framework.core.utils.sandbox.FileMediaStoreImpl
import com.zz.framework.core.utils.sandbox.IFile
import java.io.InputStream

/**
 * Copyright © 2024 zhun All rights reserved.
 * Created by Zha0Zhun on 2024/6/22 09:25.
 * @author: Zha0Zhun 1820022519@qq.com
 * @version: V1.0
 */

class FileAccessFactory private constructor() : IFile {
    companion object {
        var mFile: IFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            FileMediaStoreImpl.getInstance()
        } else {
            FileLegacyImpl.getInstance()
        }
        private val mInstance by lazy { FileAccessFactory() }
        fun getInstance(): FileAccessFactory {
            return mInstance
        }
    }

    override fun newFile(displayName: String, content: ByteArray, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.newFile(displayName, content, onSuccess, onError)
    }

    override fun newFile(displayName: String, inputStream: InputStream, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.newFile(displayName, inputStream, onSuccess, onError)
    }

    override fun deleteFile(uri: Uri, onSuccess: () -> Unit, onError: (Exception) -> Unit) {
        mFile.deleteFile(uri, onSuccess, onError)
    }

    override fun deleteFile(displayName: String, onSuccess: () -> Unit, onError: (Exception) -> Unit) {
        mFile.deleteFile(displayName, onSuccess, onError)
    }

    override fun readFile(uri: Uri, onSuccess: (ByteArray) -> Unit, onError: (Exception) -> Unit) {
        mFile.readFile(uri, onSuccess, onError)
    }

    override fun readFile(displayName: String, onSuccess: (ByteArray) -> Unit, onError: (Exception) -> Unit) {
        mFile.readFile(displayName, onSuccess, onError)
    }

    override fun queryFileByName(displayName: String, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.queryFileByName(displayName, onSuccess, onError)
    }

    override fun updateFile(uri: Uri, newContent: ByteArray, onSuccess: () -> Unit, onError: (Exception) -> Unit) {
        mFile.updateFile(uri, newContent, onSuccess, onError)
    }

    override fun updateFile(displayName: String, newContent: ByteArray, onSuccess: () -> Unit, onError: (Exception) -> Unit) {
        mFile.updateFile(displayName, newContent, onSuccess, onError)
    }

    override fun renameFile(uri: Uri, newDisplayName: String, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.renameFile(uri, newDisplayName, onSuccess, onError)
    }

    override fun renameFile(displayName: String, newDisplayName: String, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.renameFile(displayName, newDisplayName, onSuccess, onError)
    }

    override fun writeInputStreamToAlbum(inputStream: InputStream, displayName: String, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.writeInputStreamToAlbum(inputStream, displayName, onSuccess, onError)
    }

    override fun addBitmapToAlbum(displayName: String, bitmap: Bitmap, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.addBitmapToAlbum(displayName, bitmap, onSuccess, onError)
    }

    override fun mkdirs(displayName: String, directoryType: DirectoryType, onSuccess: (Uri) -> Unit, onError: (Exception) -> Unit) {
        mFile.mkdirs(displayName, directoryType, onSuccess, onError)
    }

    override fun getFileName(uri: Uri): String? {
        return mFile.getFileName(uri)
    }

}